/**
 * 自定义函数
 * */


//列表
function getIndex(){
    var uinfo = getAdminInfo();
    if (!uinfo) { return }
    var formData = {
        shop_id: uinfo.shop_id,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'weixiu/index',
        data: formData,
        success: function (res) {
            console.log(res);
            var h = '', tbody = $('tbody');
            if (res.code < 0) {
                h = '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            tbody.html(getHtml(res.data));
            $('#pageTotal').html(res.total);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//列表字符串
function getHtml(d){
    var h = '', i;
    h += '';
    for (i = 0; i < d.length; i ++) {
        h += '<tr class="text-c">';
        h += '<td>' + d[i].orderno + '</td>';
        h += '<td>' + d[i].detail + '</td>';
        h += '<td>' + d[i].shop_name + '</td>';
        //h += '<td>' + d[i].cost + '</td>';
        //h += '<td>' + d[i].money + '</td>';
        //h += '<td>' + d[i].lirun + '</td>';
        h += '<td>';
        h += '成本：' + d[i].cost + '元<br/>';
        h += '收款：' + d[i].money + '元<br/>';
        h += '利润：' + d[i].lirun + '元';
        h += '</td>';
        h += '<td>' + d[i].status_name + '</td>';
        //h += '<td>' + d[i].addtime + '</td>';
        //h += '<td>' + d[i].gotime + '</td>';
        //h += '<td>' + d[i].oktime + '</td>';
        h += '<td>';
        h += '发布时间：' + d[i].addtime + '<br/>';
        h += '开工时间：' + d[i].gotime + '<br/>';
        h += '完成时间：' + d[i].oktime;
        h += '</td>';
        h += '<td class="td-manage">';
        h += '<a href="javascript:;" class="ml-5" onclick="lay_edit(\'编辑\',\'weixiu-edit.html\', ' + d[i].id + ');">';
        h += '编辑';
        h += '<i class="Hui-iconfont">&#xe6df;</i>';
        h += '</a>';
        h += '&nbsp;&nbsp;';
        h += '<a href="javascript:;" class="ml-5" onclick="lay_del(' + d[i].id + ');" style="color: red;">';
        h += ' 删除';
        h += '<i class="Hui-iconfont"></i>';
        h += '</a>';
        h += '<br/>';
        if (d[i].status == 1) {
            h += '<a href="javascript:;" class="ml-5" onclick="lay_go(' + d[i].id + ');">';
            h += '设置开工';
            h += '</a>';
        }
        if (d[i].status == 2) {
            h += '<a href="javascript:;" class="ml-5" onclick="lay_ok(' + d[i].id + ');">';
            h += '设置完成';
            h += '</a>';
        }
        h += '<input type="hidden" id="cnt' + d[i].id +'" value="' + d[i].cnt + '"/>';
        h += '<input type="hidden" id="cost' + d[i].id +'" value="' + d[i].cost + '"/>';
        h += '<input type="hidden" id="money' + d[i].id +'" value="' + d[i].money + '"/>';
        h += '</td>';
        h += '</tr>';
    }
    return h;
}


//编辑弹窗
function lay_edit(title,url,id,w,h){
    url += '?id=' + id;
    //var tt = $('#title' + id).val();
    var cnt = $('#cnt' + id).val();
    var cost = $('#cost' + id).val();
    var money = $('#money' + id).val();
    //tt = tt ? escape(tt) : '';
    cnt = cnt ? escape(cnt) : '';
    cost = cost ? cost : '';
    money = money ? money : '';
    //url += '&title=' + tt;
    url += '&cnt=' + cnt;
    url += '&cost=' + cost;
    url += '&money=' + money;
    layer_show(title,url,w,h);
}


//获取详情
function getOne(){
    if (!id) { return }
    //$('#title')[0].value = title;
    $('#detail')[0].value = cnt;
    $('#cost')[0].value = cost;
    $('#money')[0].value = money;
}


//显示图片
function showImg(e){
    var h = '';
    h += '<img src="' + e.img + '" style="width: 150px;"/>';
    h += '<input type="hidden" id="img" value="' + e.img + '"/>';
    $('#bigShow').html(h);
}


//修改
function modify(){
    var uinfo = getAdminInfo();
    if (!uinfo) { return }
    var formData = {
        id: id, shop_id: uinfo.shop_id,
        detail: $('#detail').val(),
        cost: $('#cost').val(),
        money: $('#money').val(),
    };
    if (formData.detail == '') {
        msgPop({msg: '内容必须的'}); return
    }
    if (formData.cost == '') {
        msgPop({msg: '成本必须的'}); return
    }
    if (formData.money == '') {
        msgPop({msg: '收款必须的'}); return
    }
    console.log(formData);
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'weixiu/modify',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '操作成功'});
            setTimeout(function(){
                top.location.reload();
            }, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//删除
function lay_del(id) {
    var msg = '确认要 <b style="color: red; font-size: 20px;">删除</b> 吗？';
    layer.confirm(msg, function (index) {
        $.ajax({
            type: apiMethod,
            url: apiDomain + 'weixiu/del',
            data: {  id: id },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    msgPop({msg: res.msg});
                    return
                }
                layer.msg('已删除!', {icon: 1, time: 1000});
                setTimeout(function () {
                    top.location.reload();
                }, 500);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}


//设置开工
function lay_go(id) {
    var msg = '确认要 <b style="color: red; font-size: 20px;">开工</b> 吗？';
    layer.confirm(msg, function (index) {
        $.ajax({
            type: apiMethod,
            url: apiDomain + 'weixiu/go',
            data: {  id: id },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    msgPop({msg: res.msg});
                    return
                }
                layer.msg('成功!', {icon: 1, time: 1000});
                setTimeout(function () {
                    top.location.reload();
                }, 500);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}


//设置完成
function lay_ok(id) {
    var msg = '确认要 <b style="color: red; font-size: 20px;">完成</b> 吗？';
    layer.confirm(msg, function (index) {
        $.ajax({
            type: apiMethod,
            url: apiDomain + 'weixiu/finish',
            data: {  id: id },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    msgPop({msg: res.msg});
                    return
                }
                layer.msg('成功!', {icon: 1, time: 1000});
                setTimeout(function () {
                    top.location.reload();
                }, 500);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}